﻿@model GiftCardModel

@using Telerik.Web.Mvc.UI;

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.Telerik().TabStrip().Name("giftcard-edit").Items(x =>
{
    x.Add().Text(T("Admin.GiftCards.Info").Text).Content(TabInfo().ToHtmlString()).Selected(true);
    if (Model.Id > 0)
    {
        x.Add().Text(T("Admin.GiftCards.History").Text).Content(TabHistory().ToHtmlString());
    }

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "giftcard-edit"));
})
@helper TabInfo()
    { 
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.GiftCardType):
            </td>
            <td class="adminData">
                @Model.GiftCardType.ToString()
            </td>
        </tr>
        @if (Model.PurchasedWithOrderId.HasValue)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.PurchasedWithOrderId)
                </td>
                <td class="adminData">
                    <a href="@Url.Action("Edit", "Order", new { Id = @Model.PurchasedWithOrderId.Value })">@T("Admin.Common.View")</a>
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Amount):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Amount)  [@Model.PrimaryStoreCurrencyCode]
                @Html.ValidationMessageFor(model => model.Amount)
            </td>
        </tr>
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.RemainingAmountStr):
                </td>
                <td class="adminData">
                    @Model.RemainingAmountStr
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IsGiftCardActivated):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsGiftCardActivated)
                @Html.ValidationMessageFor(model => model.IsGiftCardActivated)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.GiftCardCouponCode):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.GiftCardCouponCode)
                @Html.ValidationMessageFor(model => model.GiftCardCouponCode)
                <button type="button" name="generateCouponCode" id="generateCouponCode" class="t-button">
                    @T("Admin.GiftCards.Fields.GiftCardCouponCode.Generate")</button>
                <script type="text/javascript">
        $(document).ready(function () {
            $('#generateCouponCode').click(function () {
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("GenerateCouponCode", "GiftCard"))",
                    success: function (data) {
                         $('#@Html.FieldIdFor(model => model.GiftCardCouponCode)').val(data.CouponCode);
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to generate code.');
                    }  
                });
            });
        });
                </script>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipientName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.RecipientName)
                @Html.ValidationMessageFor(model => model.RecipientName)
            </td>
        </tr>
        @if (Model.GiftCardType == Nop.Core.Domain.Catalog.GiftCardType.Virtual)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.RecipientEmail):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.RecipientEmail)
                    @Html.ValidationMessageFor(model => model.RecipientEmail)
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SenderName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SenderName)
                @Html.ValidationMessageFor(model => model.SenderName)
            </td>
        </tr>
        @if (Model.GiftCardType == Nop.Core.Domain.Catalog.GiftCardType.Virtual)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.SenderEmail):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.SenderEmail)
                    @Html.ValidationMessageFor(model => model.SenderEmail)
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Message):
            </td>
            <td class="adminData">
                @Html.TextAreaFor(model => model.Message)
                @Html.ValidationMessageFor(model => model.Message)
            </td>
        </tr>
        @if (Model.GiftCardType == Nop.Core.Domain.Catalog.GiftCardType.Virtual && Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.IsRecipientNotified):
                </td>
                <td class="adminData">
                    @Model.IsRecipientNotified.ToString()
                    <button type="submit" name="notifyRecipient" value="notifyRecipient" class="t-button">@T("Admin.GiftCards.Fields.IsRecipientNotified.Notify")</button>                    
                </td>
            </tr>
        }
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.CreatedOn):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.CreatedOn)
                </td>
            </tr>
        }
    </table>
}
@helper TabHistory()
    {
        var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    @(Html.Telerik().Grid<GiftCardModel.GiftCardUsageHistoryModel>()
                    .Name("usagehistory-grid")
                    .DataKeys(x =>
                    {
                        x.Add(y => y.Id).RouteKey("Id");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.UsedValue).Width(200);
                        columns.Bound(x => x.OrderId)
                            .Width(100)
                            .Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", "Order", new { id = x.OrderId }, new { }))
                            .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Order/Edit/") + "<#= OrderId #>\">" + T("Admin.Common.View").Text + "</a>");
                        columns.Bound(x => x.CreatedOn).Width(200);

                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax().Select("UsageHistoryList", "GiftCard", new { giftCardId = Model.Id });
                    })
                    .EnableCustomBinding(true))
    
}